package com.rent.reserve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.rent.apartment.mapper.ApartmentInfoMapper;
import com.rent.apartment.service.IApartmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rent.reserve.mapper.ReserveMapper;
import com.rent.reserve.domain.Reserve;
import com.rent.reserve.service.IReserveService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 租房订单Service业务层处理
 *
 * @author cooper
 * @date 2024-10-15
 */
@Service
public class ReserveServiceImpl extends ServiceImpl<ReserveMapper, Reserve> implements IReserveService {
    @Autowired
    private ReserveMapper reserveMapper;

    @Autowired
    private IApartmentInfoService apartmentInfoService;
    /**
     * 查询租房订单
     *
     * @param reserveId 租房订单主键
     * @return 租房订单
     */
    @Override
    public Reserve selectReserveByReserveId(Long reserveId) {
        return reserveMapper.selectReserveByReserveId(reserveId);
    }

    /**
     * 查询租房订单列表
     *
     * @param reserve 租房订单
     * @return 租房订单
     */
    @Override
    public List<Reserve> selectReserveList(Reserve reserve) {
        return reserveMapper.selectReserveList(reserve);
    }

    /**
     * 新增租房订单
     *
     * @param reserve 租房订单
     * @return 结果
     */
    @Override
    public int insertReserve(Reserve reserve) {
            return reserveMapper.insertReserve(reserve);
    }

    /**
     * 修改租房订单
     *
     * @param reserve 租房订单
     * @return 结果
     */
    @Override
    public int updateReserve(Reserve reserve) {
        return reserveMapper.updateReserve(reserve);
    }

    /**
     * 批量删除租房订单
     *
     * @param reserveIds 需要删除的租房订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReserveByReserveIds(Long[] reserveIds) {
        for (Long reserveId : reserveIds) {
            Reserve reserve = reserveMapper.selectReserveByReserveId(reserveId);
            if (reserve != null) {
                reserveMapper.updateApartmentStatus(reserve.getApartmentId(), "available");
            }
        }
        return reserveMapper.deleteReserveByReserveIds(reserveIds);
    }

    /**
     * 删除预约
     *
     * @param reserveId 预约ID
     * @return 影响的行数
     */
    @Transactional
    @Override
    public int deleteReserveByReserveId(Long reserveId) {
        Reserve reserve = reserveMapper.selectReserveByReserveId(reserveId);
        if (reserve != null) {
            reserveMapper.updateApartmentStatus(reserve.getApartmentId(), "available");
        }
        return reserveMapper.deleteReserveByReserveId(reserveId);
    }
}